<template>
  <div class="">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row :gutter="15">
        <!-- 搜索框 -->
        <el-col :span="8">
          <el-input v-model="queryInfo.query" clearable @clear="getGoodList" placeholder="请输入内容" class="input-with-select">
            <el-button slot="append" @click="getGoodList" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addInfo">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="goodlist" border stripe>
        <el-table-column width="35px" type="index" label="#"></el-table-column>
        <el-table-column width="400px" prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column width="80px" prop="goods_price" label="商品价格"></el-table-column>
        <el-table-column width="80px" prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column width="240px" prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column width="200px" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">修改</el-button>
            <el-button size="mini" type="danger" @click="removeGoodsById(scope.row)" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  components: {},
  data() {
    return {
      queryInfo: {
        query: '',
        // 当前页
        pagenum: 1,
        // 当前页显示的条数
        pagesize: 10
      },
      // 商品列表
      goodlist: [],
      // 总的条数
      total: 0
    }
  },
  mounted() {
    // 获取商品列表
    this.getGoodList()
    // this.goodlist = []
  },
  methods: {
    // 获取商品列表
    async getGoodList() {
      const { data: res } = await this.$http.get('/goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // console.log(res)
      // this.goodlist = res.data.goods
      this.goodlist = res.data.goods
      this.total = res.data.total
    },
    // 当选择了 每页的容纳大小的时候
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      // 重新查询数据
      this.getGoodList()
    },
    // 选择第几页
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      // 重新查询数据
      this.getGoodList()
    },
    // 删除信息
    async removeGoodsById(role) {
      // role是传过来的 每一个角色 ,id 是当前所要删除权限的id
      // 该对话框 返回的一个字符串 promise对象
      const resStr = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 判断是否确定删除
      if (resStr !== 'confirm') return this.$message.info('用户取消操作')
      const { data: res } = await this.$http.delete(`/goods/${role.goods_id}`)
      // 修改失败就阻止
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      // 重新获取数据
      this.getGoodList()
    },
    // 跳转添加信息页面
    addInfo() {
      this.$router.push('/goods/add')
    }
  }
}
</script>
<style lang="less" scoped>
</style>
